package com.xm

import scala.collection.mutable.HashSet

object FindJudge {
  def findJudge(n: Int, trust: Array[Array[Int]]): Int = {

    var cnt: Array[Int] = new Array[Int](n + 1)

    var set = new HashSet[Int]()

    var a = 0;
    // for 循环
    for (a <- 1 to n) {
      set.add(a);
    }

    for (a <- 0 to (trust.length - 1)) {
      cnt(trust(a)(1)) = cnt(trust(a)(1)) + 1
      set.remove(trust(a)(0))
    }

    var it = set.iterator
    while (it.hasNext) {
      var next = it.next()
      if (cnt(next) == n - 1) {
        return next
      }
    }
    return -1
  }

  def main(args: Array[String]): Unit = {
    var trust = new Array[Array[Int]](5)
    trust(0) = Array(1, 3)
    trust(1) = Array(1, 4)
    trust(2) = Array(2, 3)
    trust(3) = Array(2, 4)
    trust(4) = Array(4, 3)
    println(findJudge(4, trust))

  }
}
